﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
using System.Windows.Controls;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using INTEX.DataBase.Tools;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class DSTPLAN : DocumentBase
    {
        #region-------------Properties-----------------------
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        DSTPLANData m_Data, m_DataX;
        public DataSet setUI=new DataSet();
        public DataSet dsParamaters;
        public DataRow dwABPOS;
        public DataRow dwVKPOS;
        public INTEXDBC INTEXDBC;
        private string fromshippingdate = "", endshippingdate = "";
        private DataRow dwUI;
        DataTable tableUI;
        private string fromdate = "", enddate = "";   
        #endregion
        public DSTPLAN()
        {
            InitializeComponent();
            this.Name = "交期跟踪表";
           
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            base.SetUp(dicParamater);
            InitializeComponent();
            this.dicParamater = dicParamater;
            OracleDB = (OracleConnection)dicParamater["OracleDB"];
            dsParamaters = (DataSet)dicParamater["dsParamaters"];
            proxy = (IntexReptServBLClient)dicParamater["proxy"];
            orlReadCommand = OracleDB.CreateCommand();
            orlReadCommand.Parameters.Add("OrdNum", OracleType.NVarChar).Value = "";
            m_Data = new DSTPLANData();
            m_Data.OracleDB = this.OracleDB;
            m_Data.proxy = this.proxy;
            INTEXDBC = new INTEXDBC(OracleDB, proxy);
            tableUI = new DataTable("UIData");
            tableUI.Columns.Add("ORDERNO", typeof(string));
            tableUI.Columns.Add("SHIPFROMDATE", typeof(string));
            tableUI.Columns.Add("SHIPENDDATE", typeof(string));
            tableUI.Columns.Add("ORDERFROMDATE", typeof(string));
            tableUI.Columns.Add("ORDERENDDATE", typeof(string));
        }

        private void InitDataBinding()
        {
             
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                dwUI = tableUI.NewRow(); 
                InitDataBinding();
                txtORDERNO.Focus();               
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); 
            }
            return;

        }
        private void btnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                DataSet setResult = new DataSet();            
                setUI.Clear();
                setUI.AcceptChanges();       
                string ORDERNO = txtORDERNO.Text.Trim();

                if (ORDERNO == "")
                {
                    ORDERNO = "AAA";
                }
                if (fromshippingdate == "")
                {
                    fromshippingdate = "20000000";
                }
                if (endshippingdate == "")
                {
                    endshippingdate = "99999999";
                }
                if (fromdate == "")
                {
                    fromdate = "20000000";
                }
                if (enddate == "")
                {
                    enddate = "99999999";
                }


                dwUI["ORDERNO"] = ORDERNO;
                dwUI["SHIPFROMDATE"] = fromshippingdate;
                dwUI["SHIPENDDATE"] = endshippingdate;
                dwUI["ORDERFROMDATE"] = fromdate;
                dwUI["ORDERENDDATE"] = enddate;               
                setResult = m_Data.BuildDSTPLANData(dwUI);
                setUI.Merge(setResult);
                MasterDg.DataContext = setUI.Tables["VKPOS"];
                
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnPrintPreview_Click(object sender, RoutedEventArgs e)
        {
            m_DataX = new DSTPLANData(dicParamater);
            m_DataX.exAgent._app.Visible = false;
            m_DataX.OracleDB = this.OracleDB;
            m_DataX.proxy = this.proxy;
            DSTPLANReport m_rpt = new DSTPLANReport(dicParamater);
            m_rpt.dsSource = setUI;
            m_rpt.PrintType = "OpenExcel";
            m_rpt.exAgent = m_DataX.exAgent;
            m_rpt.WriteExcelReport("EXCEL");
        }

        private void FROM_SHIPPING_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                fromshippingdate = "";
                fromshippingdate = m_Data.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void END_SHIPPING_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                endshippingdate = "";
                endshippingdate = m_Data.GetDate(sender);
            }

            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void txtART_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key != System.Windows.Input.Key.Enter) return;
            btnQuery_Click(null,null);

        }

        private void FROM_ORDER_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                fromdate = "";
                fromdate = m_Data.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void END_ORDER_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                enddate = "";
                enddate = m_Data.GetDate(sender);
            }

            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }    
    }
}
